package com.ta.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.hutool.core.util.ObjectUtil;
import com.ta.gulimall.product.view.vo.SpuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ta.gulimall.product.view.entity.SpuInfoEntity;
import com.ta.gulimall.product.service.SpuInfoService;
import com.ta.gulimall.common.utils.system.PageUtils;
import com.ta.gulimall.common.utils.system.R;



/**
 * spu信息
 *
 * @author tuao
 * @email tuaoo@gmail.com
 * @date 2022-10-29 01:29:17
 */
@RestController
@RequestMapping("product/spuinfo")
public class SpuInfoController {
    @Autowired
    private SpuInfoService spuInfoService;

    /**
     * spu列表查询
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = spuInfoService.pageSpuinfos(params);

        return R.ok().put("page", page);
    }

    /**
     * 通过skuId集合查询spu商品信息
     * @param skuIds
     * @return
     */
    @PostMapping ("/listSpuInfoBySkuIds")
    public R listSpuInfoBySkuIds(@RequestBody List<Long> skuIds){
        Map<Long, SpuInfoEntity> entityMap = spuInfoService.listSpuInfoBySkuIds(skuIds);

        return R.ok(entityMap);
    }



    /**
     * 保存spu信息
     */
    @RequestMapping("/save")
    public R save(@RequestBody SpuVo spuVo){
        spuInfoService.saveSpuInfo(spuVo);

        return R.ok();
    }


    /**
     * spu商品上架
     * @param spuId
     * @return
     */
    @PostMapping("/{spuId}/up")
    public R upSpuInfo(@PathVariable("spuId") Long spuId){
        if (ObjectUtil.isNull(spuId)) {
            throw new RuntimeException("spuId不能为空");
        }
        spuInfoService.upSpuInfo(spuId);

        return R.ok();
    }












    //================以下是系统代码=================
    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        SpuInfoEntity spuInfo = spuInfoService.getById(id);

        return R.ok().put("spuInfo", spuInfo);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody SpuInfoEntity spuInfo){
		spuInfoService.updateById(spuInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		spuInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
